/** COPYRIGHT(C) 2008. Loc Phan. All Rights Reserved.
    A java class that create and manage a purse
    @author Loc Phan
    @version 1.01 2008-11-11
*/

import java.util.ArrayList;

public class Purse
{
   /**Constructs a purse
    * 
    */
   public Purse()
   {
      Size = 0;
   }
   /**Add a coin to purse
    * 
    * @param icoin Coin to be added
    */
   public void addCoin(String icoin)
   {
      Coins.add(icoin);
      Size = Coins.size();

   }
   /**Reverse order of items in purse
    * 
    */
   public void reverse()
   {
     for(int i=0;i<Size;i++)
     {
        Coins.add(Size, Coins.get(i));
     }
     for(int i=0;i<Size;i++)
        Coins.remove(0);
   }
   /**Checks if other purse has same coin
    * as this purse with the same order
    * 
    * @param other other purse
    * @return true if other purse contains same coins in the same order
    */
   public boolean sameCoins(Purse other)
   {
      boolean answer = false;
      if(Size==other.Coins.size())
      {
         String coin1;
         for(int f=0;f<Size;f++)
         {
            coin1 = Coins.get(f);
            for(int i=0;i<Size;i++)
            {
               if(coin1.equals(other.Coins.get(i)))
               {
                  answer = true;
                  break;
               }
               else answer = false;
            }
            if (!answer)
               break;
         }
      }
      
      return answer;
   }
   /**Checks if other purse has same coin
    * as this purse
    * 
    * @param other
    * @return true if other purse contains same coins
    */
   public boolean sameContents(Purse other)
   {
      boolean answer = false;
      if(Size==other.Coins.size())
      {
         String coin1;
         for(int i=0;i<Size;i++)
         {
            coin1 = Coins.get(i);
            if(coin1.equals(other.Coins.get(i)))
            {
               answer = true;
            }
            else
            {
               answer = false;
               break;
            }
         }
      }
      
      return answer;
   }
   /**Returns all ordered elements in purse
    * @return a string contains all elements in purse
    */
   public String toString()
   {
      String answer = "";
      Size=Coins.size();
      if(Size==0)
      {
         answer = "";
      }
      else
      {
         for(int i=0;i<Size;i++)
         {
            answer+=Coins.get(i)+",";
         }
         answer=answer.substring(0, answer.length()-1);
      }
      return "Purse[" +answer+"]";
   }
   /**Transfer contents from other purse to this purse
    * 
    * @param other Other Purse
    */
   public void transfer(Purse other)
   {
      Coins.addAll(Size, other.Coins);
      other.Coins.clear();
   }
   private ArrayList<String> Coins = new ArrayList<String>();
   private int Size;
}
